<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单主键" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否退款" prop="refund">
        <div>
            <input type="radio" value="1" v-model="queryParams.refund">已退款
            <input type="radio" value="0" v-model="queryParams.refund">未退款
            <input type="radio" value="null" v-model="queryParams.refund">取消
        </div>
      </el-form-item>
      <el-form-item label="退款状态" prop="refund">
        <div>
            <input type="radio" value="1" v-model="queryParams.status">等待
            <input type="radio" value="2" v-model="queryParams.status">通过
            <input type="radio" value="3" v-model="queryParams.status">驳回
            <input type="radio" value="null" v-model="queryParams.status">取消
        </div>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >批量确认退款</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单主键" align="center" prop="orderId" />
      <el-table-column label="退货原因" align="center" prop="reason" />
      <el-table-column label="是否已退款" align="center" prop="refund" >
        <template slot-scope="scope">
          <span>{{ scope.row.refund=='1'?'已退款':'未退款' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退货单状态" align="center" prop="status" >
        <template slot-scope="scope">
          <span>{{ renderStatus(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="submitFormUpdate(scope.row)"
          >确认已收款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listOrders, getOrders, updateOrders } from "@/api/system/orders";
import user from "@/store/modules/user";
export default {
  name: "Orders",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退货单信息表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        returnedOrdersId: null,
        orderId: null,
        refund: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: "订单主键不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "退货单状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询退货单信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.reason=user.state.id;
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        returnedOrdersId: null,
        orderId: null,
        reason: null,
        refund: null,
        createTime: null,
        updateTime: null,
        updateBy: null,
        status: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection
      this.single = selection.length==0
      this.multiple = !selection.length
    },
    handleUpdate() {
      this.reset();
      const returnedOrdersId = this.ids
      for(var i in returnedOrdersId)
      {
        this.submitFormUpdate(returnedOrdersId[i]);
      }
    },
    submitFormUpdate(row){

      if(row==null||row.status=="1")
      {
         this.$modal.msgSuccess("退货单："+row.returnedOrdersId+"\n未经过申请，无法确认");
         return;
      }
      if(row.status=="3")
      {
         this.$modal.msgSuccess("退货单："+row.returnedOrdersId+"\n未通过申请，无法确认");
         return;
      }
      if(row.refund=="1")
      {
         this.$modal.msgSuccess("退货单："+row.returnedOrdersId+"\n已确认退款，无法确认");
         return;
      }
      let form = {
          returnedOrdersId: row.returnedOrdersId,
          orderId: null,
          reason: null,
          refund: "1",
          createTime: null,
          updateTime: null,
          updateBy: null,
          status: null
      };

      updateOrders(form).then(response => {
         this.$modal.msgSuccess("修改成功");
         this.getList();
      });
    },
    renderStatus(status){
     let i=parseInt(status)
     if (i==1) return '未审核'
     if (i==2) return '已通过'
     return '未通过'
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
